// pages/comfirm_order/comfirm_order.js
const config = require('../../config.js');
const utils = require('../../utils/util.js');
const request = require('../../utils/request.js')
import { getVisitOrder, getArrivalOrder, editOrder } from '../../static/api/api.js'
let payWay;
var app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        isShowTextarea:true,
        isShowResult: false,
        couponParams: {},
        isShowCoupon: false,
        curTicketItem: null,
        isShowAptime:false,
        order_type:'',
        order_id:'',
        curCouponItem:null,
        apParams:{
            store_user_id:'',
            set_date:''
        },
        order_data:null,
        apDate:'',
        phone:'',
        
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.setData({
            order_type: options.order_type,
            order_id: options.order_id
        })
        console.log(this.data.order_type)
        console.log(this.data.order_id)
        this.getOrderData()
        payWay = this.selectComponent("#payWay");
       
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        this.setData({
            'phone': this.data.addrData?this.data.addrData.contact_phone : ''
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    },
    formSubmit(e){
        console.log('formId')
        console.log(e)
    },
    getOrderData(){
        let that = this;
        if (this.data.order_type == 10) {
            this.getVisitOrder()
        } else {
            this.getArrivalOrder()
        }
    },
    chooseGoodsCb(data) {
        if (this.data.order_data.goods_list.length>=2){
            setTimeout(function(){
                wx.showToast({
                    icon: 'none',
                    title: '亲，最多只能添加两个项目哦',
                })
            },500)
            return;
        }
        let key = `order_data.goods_list[${this.data.order_data.goods_list.length}]`
        this.setData({
            [key]: data
        })
        this.editOrder()
    },
    delProItem(e) {
        let that = this;
        let dataset = e.currentTarget.dataset;
        wx.showModal({
            title: '温馨提示',
            content: '确定要删除当前项目吗？',
            success: function (res) {
                if (res.confirm) {
                    let cArr=[];
                    that.data.order_data.goods_list.splice(dataset.index,1)
                    that.setData({
                        'order_data.goods_list': that.data.order_data.goods_list
                    })
                    that.editOrder()
                } else if (res.cancel) {

                }

            }
        })
        
    },
    //触发支付
    orderPay() {
        let that = this;
        wx.login({
            success: res => {
                payWay._orderPayWay({
                    orderId: this.data.order_data.order_id,
                    code: res.code,
                    pay_type: 'wechat'
                }, function() {
                    setTimeout(function(){
                        wx.reLaunch({
                            url: '/pages/door_service_orders/door_service_orders',
                        })
                    })
                })


            }
        })
    },
    goUrl(e) {
        let dataset = e.currentTarget.dataset;
        wx.navigateTo({
            url: dataset.url + '&goods_id=' + this.data.couponParams.goods_ids,
        })
        
    },
    //获取上门订单详情
    getVisitOrder(){
        let options = {
            order_id: this.data.order_id
        }
        wx.showLoading({
            title: '加载中...',
        })
        console.log('开始请求数据')
        console.log(options)
        console.log(this.data.order_id)
        getVisitOrder(options).then(response => {
            wx.hideLoading();
            let goods_ids=[];
            response.data.goods_list.forEach((el,index)=>{
                goods_ids.push(el.id)
            })
            console.log(response.data)
            this.setData({
                'order_data':response.data,
                'apParams.store_user_id': response.data.store_user_id,
                'couponParams.goods_ids': goods_ids.join(','),
                'couponParams.usable_type':this.data.order_type,
                'addrData': response.data.addr_data,
                'phone': response.data.addr_data?response.data.addr_data.contact_phone : ''
            })

        }).catch(response => {
            console.log(response)
            wx.hideLoading();
        })
    },
    //获取到店订单详情
    getArrivalOrder() {
        let options = {
            order_id: this.data.order_id
        }
        wx.showLoading({
            title: '加载中...',
        })
        getArrivalOrder(options).then(response => {
            wx.hideLoading();
            console.log(response.data)
            let goods_ids=[]
            response.data.goods_list.forEach((el, index) => {
                goods_ids.push(el.id)
            })
            this.setData({
                'order_data': response.data,
                'couponParams.goods_ids': goods_ids.join(','),
            })

        }).catch(response => {
            wx.hideLoading();
        })
    },
    comfirmPay(){
        let that = this;
        this.editOrder(function(){
            that.orderPay()
        })
    },
    editOrder(cb){
        let goods_id=[];
        this.data.order_data.goods_list.forEach((el,index)=>{
            goods_id.push(el.goods_id || el.id)
        })
        
        let nDate = this.data.order_data.order_time?new Date(this.data.order_data.order_time.replace(/-/g,'/')):''
        let options = {
            order_id: this.data.order_id,
            goods_id: goods_id.join(','),
            address_id: this.data.addrData?this.data.addrData.id : 0,
            customer_phone:this.data.phone,
            coupon_id: this.data.order_data.coupon_id,
            order_time: this.data.order_data.order_time,
            order_date: this.data.order_data.order_time?utils.formatDate(nDate,'yyyy-MM-dd'):'',
            order_hour: this.data.order_data.order_time ?utils.formatDate(nDate, 'h'):'',
            remark:this.data.remark
        }
        wx.showLoading({
            title: '加载中...',
        })
        editOrder(options).then(response => {
            wx.hideLoading();
            // this.setData({
            //     'order_data':response.data.order
            // })
            if(typeof cb == 'function'){
                cb();
                return;
            }
            this.getOrderData()

        }).catch(response => {
            wx.hideLoading();
        })
    },
    //关闭选择优惠券
    closeCouponList(params) {
        console.log(params)
        this.setData({
            'order_data.coupon_id': params.detail.item?params.detail.item.id:''
        })
        this.editOrder();
        this.setData({
            isShowCoupon: false,
            'isShowTextarea':true
        })
    },
    //触发选择优惠券
    showCounponList() {
        this.setData({
            isShowCoupon: true,
            'isShowTextarea': false
        })
    },
    //出现预约时间
    showApTime(){
        this.setData({
            isShowAptime: true,
            'isShowTextarea': false
        })
    },

    //关闭预约时间
    closeApTime(){
        this.setData({
            isShowAptime:false,
            'isShowTextarea': true
        })
    },
    gosDetail(e){
        let dataset = e.currentTarget.dataset;
        if (dataset.type == 10){
            wx.navigateTo({
                url: `/pages/craftsman_index/craftsman_index?id=${dataset.store_user_id}`,
            })
        }
        else if (dataset.type == 20) {
            wx.navigateTo({
                url: `/pages/shop_detail_index/shop_detail_index?id=${dataset.store_id}`,
            })
        }
    },
    chooseApTime(e){
        console.log(e.detail.date)
        this.setData({
            'order_data.order_time': e.detail.date
        })
        this.editOrder()
        this.closeApTime()
    },
    inputValueChange(e){
        utils.inputValueChange(e,this)
    },
    makePhoneCall(e){
        let dataset = e.currentTarget.dataset;
        wx.makePhoneCall({
            phoneNumber: dataset.phone,
        })
    },
    chooseAddrCb(data){
        let that = this;
        this.setData({
            'addrData': data,
            'phone': data.contact_phone
        })
        this.editOrder()
    }
    
})